/* 
 * Copyright (c) 2011-, Agilord Kft. - All Rights Reserved.
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at 
 * 
 * http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.agilord.dragonflyp4;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.util.Properties;

/**
 * Simple singleton configuration holder
 * 
 * @author Istvan Soos
 * @since 1.0
 */
public class Configuration {

	private static Configuration _instance = new Configuration();

	public static Configuration get() {
		return _instance;
	}

	protected Properties props;

	protected String[] extensions;
	protected String exiftool;
	protected ProcessModule[] process;

	private Configuration() {
		super();
		props = new Properties();
		load("classpath:" +
		        Configuration.class.getPackage().getName().replaceAll("\\.", "/") + "/defaults.properties");
		props.putAll(System.getProperties());
	}

	public void init(String... args) {
		for (String arg : args) {
			if (arg.endsWith(".properties"))
				load(arg);
		}

		this.extensions = ConfigurationUtils.parseCommas(props.getProperty("extensions"));
		this.exiftool = ConfigurationUtils.selectFirstFile(props.getProperty("exiftool"));

		String[] processSeparated = ConfigurationUtils.parseCommas(props.getProperty("process"));
		this.process = new ProcessModule[processSeparated.length];
		for (int i = 0; i < processSeparated.length; i++) {
			try {
				process[i] = (ProcessModule) Class.forName(
				        props.getProperty("module." + processSeparated[i]).trim()).newInstance();
			} catch (InstantiationException e) {
				throw new RuntimeException(e);
			} catch (IllegalAccessException e) {
				throw new RuntimeException(e);
			} catch (ClassNotFoundException e) {
				throw new RuntimeException(e);
			}
		}
	}

	public void load(String name) {
		if (name.startsWith("classpath:")) {
			try {
				load(getClass().getClassLoader().getResourceAsStream(name.substring(10)));
			} catch (IOException e) {
				throw new RuntimeException(e);
			}
		} else if (new File(name).exists()) {
			try {
				load(new FileInputStream(name));
			} catch (IOException e) {
				throw new RuntimeException(e);
			}
		} else {
			throw new RuntimeException("Unable to load properties files: " + name);
		}
	}

	protected void load(InputStream is) throws IOException {
		Properties p = new Properties();
		p.load(is);
		props.putAll(p);
	}
	
	public String[] parseCommas(String key) {
		return ConfigurationUtils.parseCommas(props.getProperty(key));
	}

	public String[] getExtensions() {
		return extensions;
	}

	public String getExiftool() {
		return exiftool;
	}

	public ProcessModule[] getProcess() {
		return process;
	}

	public Properties getProps() {
		return props;
	}

}
